﻿/*==============================================================
 * ArticleService.cs
 * Copyright (C) 2014 贵州惠众互联 Inc. All rights reserved.
 *==============================================================
 * Author:   罗应红
 * Date:     2014/03/06 15:47:33.
 * Version:  1.0
 * QQ:		 88962800
*==============================================================*/

using System.Collections.Generic;
using System.Linq;
using QDT.Common;
using QDT.Common.Data;
using QDT.Core;
using QDT.Core.Domain;
using QDT.Core.Domain.Enums;

namespace QDT.Services.ArticleMoudule
{
    public class ArticleService : ServiceBase, IArticleService
    {
        /// <summary>
        /// 通过分类获得分类页模版
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string GetCateTemp(int cid)
        {
            var type = DbProvider.GetByID<ArticleCate>(3);
            switch (type.TypeId)
            {
                case ArticleType.Media:
                    return "_AritcalCateMedia";
            }
            return "_ArticalCate";
        }

        /// <summary>
        /// 通过分类获得文章详情模版
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public string GetDetailTemp(int cid)
        {
            var type = DbProvider.GetByID<ArticleCate>(cid);
            switch (type.TypeId)
            {
                case ArticleType.Media:
                    return "_AritcalDetailMedia";
            }
            return "_ArticalDetail";
        }

        /// <summary>
        /// 根据分类获得文章列表
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public IPagedList<Article> QueryList(int page, int size)
        {
            var source = DbProvider.D<Article>().Where(x => x.Cate.InnerArticleTypeId == (int)ArticleType.Media && x.IsEffect && !x.IsDelete).OrderByDescending(x => x.CreateTime);
            return source.ToPagedList(page - 1, size);
        }

        public IPagedList<Article> QueryArticle(int page, int size, ArticleType type)
        {
            int total;
            var data = DbProvider.Repository<Article>().Filter(o => o.CreateTime,
                                                           o =>
                                                           !o.IsDelete && o.IsEffect
                                                           && o.Cate.InnerArticleTypeId == (int)type, out total,
                                                           SortingOrders.Desc, page - 1, size);
            return new StaticPagedList<Article>(data, page - 1, size, total);
        }

        public IList<Article> QueryLatestArticle(int count)
        {
            var data =
                DbProvider.D<Article>().Where(o => o.IsEffect && !o.IsDelete && (o.Cate.InnerArticleTypeId == (int)ArticleType.Media)).OrderByDescending(
                    o => o.CreateTime).Take(count);
            return data.ToList();
        }

        public IList<Article> SingleNew()
        {
            var data =
                DbProvider.D<Article>().Where(o => o.IsEffect && !o.IsDelete && (o.Cate.InnerArticleTypeId == (int)ArticleType.Notice)).OrderByDescending(o => o.CreateTime).Take(4);
            return data.ToList();
        }
        public Article GetArticleById(int id)
        {
            var data = DbProvider.GetByID<Article>(id);
            return data;
        }



    }
}